﻿using System;
using System.Collections.Generic;
using csvm.fileadapter.contract;
using System.IO;

namespace csvm.fileadapter
{
    public class FileAdapter : IFileAdapter
    {
        public IEnumerable<string> ReadFile(string filename)
        {
            StreamReader StreamReader = File.OpenText(filename);

            string Line;
            long ReadBytes = 0;

            while ((Line = StreamReader.ReadLine()) != null)
            {
                ReadBytes += Line.Length;
                OnProgressChanged(GetPercentageOfReadBytes(StreamReader.BaseStream.Length, ReadBytes));
                yield return Line;
            }
        }

        private double GetPercentageOfReadBytes(long FileSize, long CurrentSize)
        {
            return (double)CurrentSize/(double)FileSize*100.0;
        }

        public event Action<double> OnProgressChanged = delegate (double obj){};
    }
}
